package com.miracle.main.module.mvp.presenter;

import android.app.Activity;
import android.support.v4.app.Fragment;

import com.miracle.basemvp.base.BasePresenter;
import com.miracle.basemvp.base.BaseResponse;
import com.miracle.basemvp.base.IPresenter;
import com.miracle.basemvp.base.state.StateController;
import com.miracle.basemvp.lifecycle.rx.RxDefaultObserver;
import com.miracle.common.bean.User.User;
import com.miracle.common.mvp.entity.response.BaseListResponse;
import com.miracle.common.util.CommonConstants;
import com.miracle.common.util.PreferencesUtils;
import com.miracle.main.module.mvp.contract.FileListContract;
import com.miracle.main.module.mvp.entity.bean.FileItem;
import com.miracle.main.module.mvp.entity.request.FileListRequest;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;

import javax.inject.Inject;

import static com.miracle.common.util.CheckUtil.checkListIsEmpty;
import static com.miracle.main.module.mvp.ui.fragment.FileListFragment.TYPE_PIC;

/**
 * com.miracle.main.module.mvp.presenter
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/11 10:18
 * @see com.miracle.main.module.mvp.presenter
 */
public class FileListPresenter extends BasePresenter<FileListContract.View, FileListContract
        .Model> implements IPresenter {

    @Inject
    public FileListPresenter(FileListContract.View view, FileListContract.Model model) {
        super(view, model);
    }

    public void getFileList(LifecycleProvider<FragmentEvent> lifecycleProvider, int type, final
    StateController stateController) {
        Activity activity = ((Fragment) mView).getActivity();
        String jessionId = PreferencesUtils.getPreferences(activity, PreferencesUtils.State
                .USER_SESSIONID);
        FileListRequest request = new FileListRequest();
        request.setUri(CommonConstants.URI.URI_FILE_LIST);
        request.setJSESSIONID(jessionId);
        request.setPageNo(1);
        if(type == TYPE_PIC){
            request.setFileType("photos");
        }else{
            request.setFileType("");
        }
        request.setPageSize(100);

        mModel.getFileList(request)
                .compose(lifecycleProvider.<BaseResponse<BaseListResponse<FileItem>>>bindUntilEvent
                        (FragmentEvent.DESTROY))
                .subscribe(new RxDefaultObserver<BaseResponse<BaseListResponse<FileItem>>>
                        (stateController) {
                    @Override
                    public void onSucceed(BaseResponse<BaseListResponse<FileItem>> arrayListBaseResponse) {
                        if (arrayListBaseResponse != null) {
                            ArrayList<FileItem> list = arrayListBaseResponse.getContent().getList();
                            if (checkListIsEmpty(list)) {
                                stateController.onNoDataState();
                            } else {
                                stateController.onSuccessState();
                                mView.getFileListSucc(list);
                            }
                        }
                    }
                });
    }
}
